package com.airsaid.pickerviewlibrary.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.R;
import com.airsaid.pickerviewlibrary.adapter.NumericWheelAdapter;
import com.airsaid.pickerviewlibrary.adapter.WheelAdapter;
import com.airsaid.pickerviewlibrary.listener.OnItemSelectedListener;
import com.airsaid.pickerviewlibrary.widget.wheelview.WheelView;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 选择时分十二小时制
 *
 * @author on 2022/9/23
 */
public class ChoiceHoursMinuteBy12View extends BasePickerView implements View.OnClickListener{



    private WheelView mWheelView,wv_hours ,wv_minute;

    public TextView tvTitle ;

    /**
     * 上下午
     */
    private List<String> list = new ArrayList<>();

    /**
     * 上午还是下午
     */
    public int type = 0 ;

    /**
     * 时
     */
    public int checkHour = 0 ;

    /**
     * 分
     *
     */
    public int checkMinute = 0 ;



    public OnSelectListener onSelectListener;



    public ChoiceHoursMinuteBy12View(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.choice_hour_and_minute_by_12_layout,contentContainer);
        initView();
        initData();
    }


    public void initView() {
        wv_hours = (WheelView) findViewById(R.id.wv_hours);
        wv_minute = (WheelView) findViewById(R.id.wv_minute);
        mWheelView = (WheelView) findViewById(R.id.mWheelView);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        findViewById(R.id.btnCancel).setOnClickListener(this);
        findViewById(R.id.btnSubmit).setOnClickListener(this);
    }


    private void initData() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);

        if (Calendar.AM == calendar.get(Calendar.AM_PM)){
            type = 0 ;
        }else {
            type = 1 ;
        }

        list.add("上午");
        list.add("下午");
        mWheelView.setCurrentItem(type);
        mWheelView.setAdapter(new WheelAdapter() {
            @Override
            public int getItemsCount() {
                return list.size();
            }

            @Override
            public Object getItem(int index) {
                return list.get(index);
            }

            @Override
            public int indexOf(Object o) {
                return list.indexOf(o);
            }
        });


        wv_hours.setAdapter(new NumericWheelAdapter(1, 12));
        wv_hours.setLabel("时");// 添加文字
        wv_hours.setCurrentItem(hour-1);
        checkHour = hour;


        wv_minute.setAdapter(new NumericWheelAdapter(0, 59));
        wv_minute.setLabel("分");// 添加文字
        wv_minute.setCurrentItem(minute);
        checkMinute = minute;


        mWheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                type = index ;
            }
        });

        wv_hours.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Log.v("[ItemSelected]->"," wv_hours index = "+index);
                checkHour = index+1 ;
            }
        });

        wv_minute.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Log.v("[ItemSelected]->"," wv_minute index = "+index);
                checkMinute = index ;
            }
        });
    }



    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnCancel) {
            dismiss();
        }else if (v.getId() == R.id.btnSubmit){
            if (onSelectListener!=null){
                onSelectListener.onCitySelect(type,checkHour,checkMinute);
                dismiss();
            }
        }
    }



    /**
     * 设置默认选中
     * @param type
     * @param hour
     * @param minute
     */
    public ChoiceHoursMinuteBy12View setPicker(int type,int hour, int minute){
        if (hour!=0){
            this.type = type ;
            this.checkHour = hour ;
            this.checkMinute = minute ;
            setCurrentItem();
        }
        return this;
    }






    private void setCurrentItem(){
        mWheelView.setCurrentItem(type);
        wv_hours .setCurrentItem(checkHour-1);
        wv_minute.setCurrentItem(checkMinute);
    }



    /**
     * 设置文字大小
     *
     * @param size
     * @return
     */
    public ChoiceHoursMinuteBy12View setTextSize(int size){
        mWheelView.setTextSize(size);
        wv_hours.setTextSize(size);
        wv_minute.setTextSize(size);
        return this ;
    }



    /**
     * 是否重复滚动
     *
     * @param b
     * @return
     */
    public ChoiceHoursMinuteBy12View setCyclic(boolean b){
        wv_hours.setCyclic(b);
        wv_minute.setCyclic(b);
        return this ;
    }



    /**
     * @param hourLabel
     * @return
     */
    public ChoiceHoursMinuteBy12View setLabel(String hourLabel,String minuteLabel){
        wv_hours.setLabel(hourLabel);
        wv_minute.setLabel(minuteLabel);
        return this ;
    }





    /**
     * 设置标题
     */
    public ChoiceHoursMinuteBy12View setTitle(String title){
        if (!TextUtils.isEmpty(title)){
            tvTitle.setText(title);
        }
        return this ;
    }



    public interface OnSelectListener {
        /**
         * @param type 0（上午）、 1（下午）
         * @param hour 1 - 12
         * @param minute 0 - 59
         */
        void onCitySelect(int type,int hour, int minute);
    }


    /**
     * 当前选择时间
     *
     * @param onSelectListener
     */
    public ChoiceHoursMinuteBy12View setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
        return this ;
    }
}
